﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SD.Business;
using SD.DataAccess;

//11:34 12 Dec 2012
namespace SD.Web.Iteration1.Contacts
{
    public partial class AddContact : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //this.Literal1.Text = ContactLookup.ContactID.ToString();

                // load data for drop down list best contact method
                ddlBestContactMethod.DataSource = ContactBUS.SelectAllBestMethodContacts();
                ddlBestContactMethod.DataBind();
                ddlBestContactMethod.DataTextField = "BestContactMethodName";
                ddlBestContactMethod.DataValueField = "BestContactMethodId";
                ddlBestContactMethod.Items.Insert(0, new ListItem(""));

                // load data for drop down list contact type
                ddlContactType.DataSource = ContactBUS.SelectAllContactTypes();
                ddlContactType.DataBind();
                ddlContactType.DataTextField = "ContactTypeName";
                ddlContactType.DataValueField = "ContactTypeId";
                ddlContactType.Items.Insert(0, new ListItem("", "0"));
            }
        }

        protected void LinkButtonLookupClick(object sender, EventArgs e)
        {
            ContactLookup.Visible = true;
            ContactLookup.Popup();
        }

        protected void btnSaveClick(object sender, EventArgs e)
        {
            try
            {
                // set value for contact
                Contact contact = new Contact();
                contact.FirstName = txtFirstName.Text.Trim();
                contact.Surname = txtSurname.Text.Trim();
                contact.KnownAs = txtKnowAs.Text.Trim();
                
                contact.OfficePhone = txtOfficePhone.Text.Trim();
                contact.MobilePhone = txtMobilePhone.Text.Trim();
                contact.HomePhone = txtHomePhone.Text.Trim();
                contact.Email = txtEmailAddress.Text.Trim();
                
                contact.ManageId = ContactLookup.ContactId;
                contact.ContactTypeId = Convert.ToInt32(ddlContactType.SelectedValue);
                if (string.IsNullOrEmpty(ddlBestContactMethod.SelectedValue) == false)
                {
                    contact.BestContactMethodId = Convert.ToInt32(ddlBestContactMethod.SelectedValue);    
                }
                contact.JobRole = txtRole.Text.Trim();
                contact.JobTitle = txtJobTitle.Text.Trim();
                contact.WorkBase = txtWorkBase.Text.Trim();
                contact.IsActive = CheckBoxIsActive.Checked;
                bool result = ContactBUS.InsertContact(contact); // insert
                if (result)
                {
                    //Response.Redirect("ListContact.aspx");
                    Literal1.Text = "Add contact successfully.";
                }
                else
                {
                    Literal1.Text = "Please check information of contact.";
                }
            }
            catch (Exception ex)
            {
                Literal1.Text = ex.Message;
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("ListContact.aspx");
        }
    }
}